const api = require('../../api/api')
const app = getApp()
const util = require('../../utils/js/util')
Page({
  data: {
    userInfo: app.globalData.userInfo,
    safebottom: util.getSafeToBottom(),
    query: '',
    page: 1,
    list: [],
    listStatus: 'loading',
  },
  onLoad(options) {
    options?.query && this.setData({ query: options.query === '更多' ? '' : options.query })
    this.getlist();
  },
  onReachBottom() {
    if(this.data.listStatus === 'loading') {
      this.setData({ page: this.data.page + 1 })
      this.getlist()
    }
  },
  confirm() {
    this.setData({ listStatus: 'loading', page: 1 })
    this.getlist()
  },
  async getlist() {
    const res = await api.getServiceList({ query: this.data.query, p: this.data.page })
    const newdata = (res?.data || []).map(v => ({
      ...v,
      tags: v.tags.split('/')
    }))
    this.setData({
      list: this.data.page == 1 ? newdata : this.data.list.concat(newdata),
      listStatus: res.current_page == res.last_page ? 'tobottom' : res.current_page < res.last_page ? 'loading' : res.last_page < res.current_page ? 'nodata' : ''
    })
  },
  gotoDoctorDetail({ currentTarget: { dataset: { id } } }) {
    wx.navigateTo({ url: '../doctor/index?id=' + id })
  },
})
